package com.yz.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yz.common.annotation.Login;
import com.yz.common.annotation.LoginUser;
import com.yz.common.entity.BookEntity;
import com.yz.common.entity.UserEntity;
import com.yz.common.utils.R;
import com.yz.common.validator.ValidatorUtils;
import com.yz.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 *文献相关操作
 */
@RestController
@RequestMapping("/api")
@Api(tags="文献")
public class ApiBookController {
    @Autowired
    private BookService bookService;

    @GetMapping("getBookDetail")
    @ApiOperation("查看文献详情")
    @Login
    public R getBookDetail(@RequestHeader("token")String token,@RequestParam("id") long id){
        BookEntity t = bookService.selectOne(new EntityWrapper<BookEntity>().eq("id",id));
        return R.ok().put("data",t);
    }
    @DeleteMapping("deleteBook")
    @ApiOperation("删除文献")
    @Login
    public R deleteBook(@RequestHeader("token")String token,@RequestParam("id") long id){
        bookService.deleteById(id);
        return R.ok().put("data",null);
    }
    @PostMapping("postBook")
    @ApiOperation("提交或修改文献")
    @Login
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "0草稿1未审核2已审核", dataType = "int")})
    public R postBook(@RequestHeader("token")String token,@RequestBody BookEntity t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        bookService.insertOrUpdate(t);
        return R.ok().put("data",null);
    }

    @GetMapping("getBookList")
    @ApiOperation("获取文献列表")
    @Login
    public R getBookList(@RequestHeader("token")String token,@RequestParam(value = "keyWord",required = false) String keyWord,@RequestParam("order")int order,@RequestParam("type") int type,@RequestParam("page") int page,@RequestParam("pageSize") int pageSize, @ApiIgnore @LoginUser UserEntity user){
        R r= new R();
        Page<BookEntity> logs=null;
        int total =0;
        String group = "update_at desc,id desc";
        if(order!=0){
            group = "update_at asc,id asc";
        }
        if(keyWord==null||keyWord.isEmpty()){
            logs = bookService.selectPage(new Page<BookEntity>(page+1,pageSize),new EntityWrapper<BookEntity>().eq("type",type).orderBy(group));
            total = bookService.selectCount(new EntityWrapper<BookEntity>().eq("type",type));

        }else{
            logs = bookService.selectPage(new Page<BookEntity>(page+1,pageSize),new EntityWrapper<BookEntity>().eq("type",type).where("(title like '%"+keyWord+"%' or writer like '%"+keyWord+"%' or tip like '%"+keyWord+"%' or book_name like '%"+keyWord+"%' or meet like '%"+keyWord+"%' or applicant like '%"+keyWord+"%' or inventor like '%"+keyWord+"%' )").orderBy(group));
            total = bookService.selectCount(new EntityWrapper<BookEntity>().eq("type",type).where("(title like '%"+keyWord+"%' or writer like '%"+keyWord+"%' or tip like '%"+keyWord+"%' or book_name like '%"+keyWord+"%' or meet like '%"+keyWord+"%' or applicant like '%"+keyWord+"%' or inventor like '%"+keyWord+"%' )"));
        }
        r.put("msg",total+"");
        r.put("code",0);
        r.put("data",logs.getRecords());
        r.put("total",total);
        return r;
    }
}
